package ioperation;
import book.Book;
import book.BookList;
import exception.AddException;
import java.util.Scanner;

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList) throws AddException{

        //创建新的书籍
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入种类：");
        String type = scanner.nextLine();
        System.out.println("请输入价格：");
        int price = scanner.nextInt();
        Book newbook = new Book(name,author,price,type,false);

        //判断是否已经存在
        for (int i = 0; i < bookList.getUsedsize(); i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                throw new AddException("该书本已经存在，添加失败.....");
            }
        }

        //添加到ArrayList中
        bookList.setBook(newbook);
        System.out.println("添加成功！！");
    }

}